package com.java.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.java.dao.OrderDao;
import com.java.model.Company;
import com.java.model.Order;
import com.java.model.PageBean;
import com.java.model.User;
import com.java.util.PriceUtil;
import com.java.util.StringUtil;

/**
 * department
 * 
 * @author ld
 *
 */
@Repository("orderDao")
public class OrderDaoImpl implements OrderDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Order> find(PageBean pageBean, Order s_order) {
		System.out.println("Order:" + s_order.toString());
		StringBuffer sb = new StringBuffer(
				"select a.*, b.name companyName from t_order a left join t_company b on a.companyId = b.id");
		if (s_order != null) {
			if (null != s_order.getUserId()) {
				sb.append(" and a.userId = '" + s_order.getUserId() + "'");
			}
			if (null != s_order.getId()) {
				sb.append(" and a.id like '%" + s_order.getId() + "%'");
			}
			if (null != s_order.getCompanyName()) {
				sb.append(" and b.name like '%" + s_order.getCompanyName() + "%'");
			}
		}
		if (pageBean != null) {
			sb.append(" limit " + pageBean.getStart() + "," + pageBean.getPageSize());
		}

		System.out.println(sb.toString().replaceFirst("and", "where"));

		final List<Order> orderList = new ArrayList<Order>();
		jdbcTemplate.query(sb.toString().replaceFirst("and", "where"), new Object[] {}, new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Order order = new Order();
				order.setId(rs.getInt("id"));
				order.setUserId(rs.getInt("userId"));
				order.setCompanyId(rs.getInt("companyId"));
				order.setCreateTime(rs.getDate("createTime"));
				order.setAddress(rs.getString("address"));
				order.setToAddress(rs.getString("toAddress"));
				order.setWeight(rs.getString("weight"));
				order.setPrice(rs.getString("price"));
				order.setCompanyName(rs.getString("companyName"));
				orderList.add(order);
			}
		});
		return orderList;
	}

	@Override
	public int count(Order s_order) {
		StringBuffer sb = new StringBuffer("select count(a.id) from t_order a left join t_company b on a.companyId = b.id");
		if (s_order != null) {
			if (null != s_order.getUserId()) {
				sb.append(" and a.userId = '" + s_order.getUserId() + "'");
			}
			if (null != s_order.getId()) {
				sb.append(" and a.id like '%" + s_order.getId() + "%'");
			}
			if (null != s_order.getCompanyName()) {
				sb.append(" and b.name like '%" + s_order.getCompanyName() + "%'");
			}
		}
		return jdbcTemplate.queryForObject(sb.toString().replaceFirst("and", "where"), Integer.class);
	}

	@Override
	public void add(Order order) {
		System.out.println("add:order:" + order);
		String sql = "insert into t_order values(null,?,?,now(),?,?,?,?)";
		String cprice = null;
		if (order.getPriceType().equals("1")){
			cprice = PriceUtil.calPrice(order.getAddress(), order.getToAddress(), order.getWeight());
		}else{
			cprice = PriceUtil.calPrice2(order.getAddress(), order.getToAddress(), order.getWeight());
		}
		order.setPrice(cprice);
		System.out.println("add:"+sql);
		jdbcTemplate.update(sql, new Object[] { order.getUserId(), order.getCompanyId(), order.getAddress(),
				order.getToAddress(), order.getWeight(), order.getPrice() });
	}

	// @Override
	// public void update(Order order) {
	// String sql="update t_order set logistics=?,remark=? where id=?";
	// jdbcTemplate.update(sql, new
	// Object[]{department.getDeptName(),department.getRemark(),department.getId()});
	// }

	@Override
	public Order loadById(int id) {
		String sql = "select * from t_order where id=?";
		final Order order = new Order();
		jdbcTemplate.query(sql, new Object[] { id }, new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				order.setId(rs.getInt("id"));
				order.setUserId(rs.getInt("userId"));
				order.setCompanyId(rs.getInt("companyId"));
				order.setCreateTime(rs.getDate("createTime"));
				order.setAddress(rs.getString("address"));
				order.setToAddress(rs.getString("toAddress"));
				order.setWeight(rs.getString("weight"));
				order.setPrice(rs.getString("price"));
			}
		});
		return order;
	}

	@Override
	public void update(Order order) {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(int id) {
		String sql = "delete from t_order where id=?";
		jdbcTemplate.update(sql, new Object[] { id });
	}

}
